﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LevelEditor.LERes
{
    public partial class NewMap : Form
    {
        private List<Bitmap> imgList = new List<BitMap>();
        private string imgPath;
        private LevelEditor frmLE;
        public NewMap()
        {
            InitializeComponent();
        }

        public NewMap(LevelEditor leObj)
        {
            InitializeComponent();
            frmLE = leObj;
        }


        private void NewMap_Load(object sender, EventArgs e)
        {
            // We should handle if user is working with map. And he want to create another one
            // We have 2 choice:
            // 1. Close the previous map and create new one
            // 2. Support tab menu for multi map editor

            // Do nothing right now

            

        }


        /// <summary>
        /// Reset all data in text box
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnReset_Click(object sender, EventArgs e)
        {         
            txtTileSize.Text = "";
            txtWidth.Text = "";
            txtHeight.Text = "";
        }
        // End btnReset_Click()


        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOK_Click(object sender, EventArgs e)
        {
            string sTileSize = txtTileSize.Text.Trim();
            string sWidth = txtWidth.Text.Trim();
            string sHeight = txtHeight.Text.Trim();

            int tileSize = int.Parse(sTileSize);
            int width = int.Parse(sWidth);
            int height = int.Parse(sHeight);

            if (sTileSize.Length == 0 || sWidth.Length == 0 || sHeight.Length == 0)
            {
                MessageBox.Show("Please check your input data");
            }
            else
            {
                // Preprocess image, split into a list of smaller tile

                ImagePreprocess(tileSize);
                // Do nothing right now
                frmLE.GetData(tileSize, width, height);

            }
        }
        // End btnOK_Click()

        /// <summary>
        /// Split image into a list of smaller images
        /// </summary>
        /// <param name="tileSize"></param>
        private void ImagePreprocess(int tileSize)
        {
            int idX, idY;
            int width, height;
            Image imgSrc = Image.FromFile(imgPath);

            Bitmap cropImage;

            Rectangle cropArea = new Rectangle();
            
            for (idY = 0; idY < height; idY++)
            {
                for (idX = 0; idX < width; idX++)
                {
                    cropArea.X = tileSize * idX;
                    cropArea.Y = tileSize * idY;
                    cropArea.Width = tileSize;
                    cropArea.Height = tileSize;

                    cropImage = imgSrc.Clone(cropArea, imgSrc.PixelFormat);
                    // Add to list
                    imgList.Add(cropImage);
                }
            }
        }


    }
}
